package com.example.webapp.controler;

import com.example.webapp.bean.Registration;
import com.example.webapp.service.RegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class SearchTestControler {
    @Autowired
    private RegistrationService registrationService;

    @CrossOrigin
    @PostMapping("/searchtest")
    @ResponseBody
    public String answer(@RequestBody Map map){
        String UidStr=(String)map.get("uid");
        Integer Uid=Integer.parseInt(UidStr);

        List<Registration> StudentRegistration=registrationService.findRegistrationsByStudent_Id(Uid);
        String TestObjectsStr="[";
        for(int i=0;i<StudentRegistration.size();i++){
            TestObjectsStr+= StudentRegistration.get(i).Serialization();
            if(i!=StudentRegistration.size()-1){
                TestObjectsStr+=",";
            }
        }
        TestObjectsStr+="]";
        return TestObjectsStr;
    }

}